GBML manual / Grids

grid_create

Makes a grid of width columns and height rows, every cell starting as fill. The kind of grid follows from fill: grid_create(8, 6, 0) is an 8-wide, 6-tall grid. width and height must be whole numbers written right there in the code.

Syntax

grid_create(width, height, fill) -> grid<T>

Arguments

NameType
widthint
heightint
fillelement

Returns

grid<T>

Description

Makes a grid width cells across and height cells down, and puts fill in every cell. grid_create(8, 6, -1) is a board of 8 columns and 6 rows where every square starts as -1.

The kind of grid follows from fill (0 makes a grid<int>, 0.0 a grid<real>, and so on), so there is nothing else to say.

So write fill as the kind you want. grid_create(8, 6, 0) is a grid of whole numbers, and a decimal does not fit in one - board[# 2, 3] = 0.5 is refused when the project is checked, and the message names the grid_create line that made the grid hold whole numbers. If the grid will ever hold a decimal, make it with grid_create(8, 6, 0.0). The other way is fine: a whole number goes into a grid of decimals as itself.

width and height have to be numbers you type, like 8 and 6. Not variables. A GBA cannot hand out memory while a game runs, so a grid is given its room when the game is built - see Writing a type out for the full reason. If you use a variable, the compiler explains it.

It sets the size of the variable it is given to, so it must be the whole right-hand side of the line that creates the variable (anywhere else is refused): var board = grid_create(8, 6, -1);, or board = grid_create(8, 6, -1); in Create for an instance variable.

Why bother? Writing a grid as var board: grid<int, 8, 6> = []; fills every cell with the type's default (0), and that is all it can do. If you want a board that begins full of -1, meaning "empty square", grid_create is the way that needs no loop. It also keeps the whole size on one line: the two numbers are the width and the height, in that order.

Example

var board = grid_create(8, 6, -1);   // 8 across, 6 down, every square -1
board[# 2, 1] = 3;                   // column 2, row 1
var square = board[# 2, 1];          // 3

On the GBA

Compiles to a fixed-size vector of width*height cells filled by the GBA module's own count-and-value constructor, indexed row by row (the same storage `/// @capacity WxH` gives). Unlike a bare `[]`, which fills every cell with the type's default (0, 0.0, false), this lets the cells start as anything - a board that begins full of -1 for "empty", say. width and height have to be plain numbers because the grid's size is a C++ template parameter: the Game Boy Advance has no heap, so it is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration.

See also: grid_widthgrid_heightgrid_fillgrid_fill_regiongrid_copyarray_createGridsWriting a type out, instead of in a commentData containers